
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[EBAS_SP_HENT_KARAKTER_SKALA]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[EBAS_SP_HENT_KARAKTER_SKALA]
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE [DBO].[EBAS_SP_HENT_KARAKTER_SKALA] 
	@session varchar(255),
	@navn varchar(255) = NULL,
	@fagGuid varchar(255) = NULL
WITH ENCRYPTION
AS
	DECLARE @rettighed int,
		@antal int, 
		@guid uniqueidentifier
	
	SET NOCOUNT ON
	SET @antal = 0

	CREATE TABLE #temp ( rett int )
	INSERT INTO #temp (rett)
	EXEC EBAS.DBO.EBAS_SP_SIKKERHED_VERIFICER_SESSION @session	

	SET @rettighed = (SELECT IsNull(MAX(rett),0) FROM #temp)
	
	IF 0 < @rettighed 
	BEGIN
		SET @antal = (SELECT COUNT(*) FROM EBAS_KARAKTER_SKALA)
		
		IF @navn IS NULL AND @fagGuid IS NULL
			SELECT 	GUID, NAVN, @antal AS ANTAL
			FROM 	EBAS_KARAKTER_SKALA
		ELSE IF @navn IS NOT NULL AND @fagGuid IS NULL
			SELECT 	GUID, NAVN, @antal AS ANTAL
			FROM 	EBAS_KARAKTER_SKALA
			WHERE	NAVN = @navn
		ELSE IF @fagGuid IS NOT NULL
			SELECT 	GUID, NAVN, @antal AS ANTAL
			FROM 	EBAS_KARAKTER_SKALA
			WHERE	GUID IN (
				SELECT KARAKTER_SKALA_GUID
				FROM EBAS_FAG
				WHERE GUID = @fagGuid			
			)
	END
	ELSE			
		SELECT 	GUID, NAVN, 0 AS ANTAL
		FROM 	EBAS_KARAKTER_SKALA
		WHERE 	GUID IS NULL
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO
